<%@ page pageEncoding="UTF-8"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@include file="../../tld.jsp"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>管理收货地址</title>
    <meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" name="viewport" />
    <meta content="yes" name="apple-mobile-web-app-capable" />
    <meta content="black" name="apple-mobile-web-app-status-bar-style" />
    <meta content="telephone=no" name="format-detection" />
    <!-- 引入公用样式 -->
    <link rel="stylesheet" href="/static/sass/common/ydui.css" />
    <link rel="stylesheet" href="/static/sass/common/dropload.css" />
    <link rel="stylesheet" href="/static/stylesheets/user/address.css" />
    <style>
        
    </style>
    <!-- 引入自适应解决方案类库 -->
    <script src="/static/scripts/libs/ydui.flexible.js"></script>
</head>
<body>
    <div class="g-flexview">
    	<input type="hidden" id="goodsId" value="${goodsId }">
        <!-- 主体内容开始 -->
        <section class="content_box content" style="margin-bottom: 50px;">
            <div class="lists">
            <c:if test="${fn:length(addressList) > 0}">
               <c:forEach items="${addressList}" var="item" varStatus="vs">
	            <!-- 列表 -->
                <div class="address-item">
                    <div class="adr-info" addressId="${item.id }">
                        <div class="content_lists">
                            <span class="name">
                               ${item.name }
                            </span>
                            <span class="phonenum">
                                ${item.mobile }
                            </span>
                        </div>
                        <div class="adr-detail">
                            ${item.preAddress}${item.address }
                        </div>
                    </div>
                    <div class="adr_action">
                        <div class="selected">
                        <c:if test="${item.isDefault == 1 }">
                              <div id="selDefault" class="selected selected_item on" addressId="${item.id }"></div>
                        </c:if>
                        <c:if test="${item.isDefault == 0 }">
                              <div id="selDefault" class="selected selected_item" addressId="${item.id }"></div>
                        </c:if>
                              <label>默认地址</label>
                        </div>
                        <a href="javascript:;" url="${base}/address/getinfo?id=${item.id }" class="edit">
                            <i class="icon-compose"></i> 编辑
                        </a>
                        <div class="del" addressId="${item.id }">
                            <i class="icon-delete"></i>删除
                        </div>
                    </div>
                </div>
           	</c:forEach>
           	</c:if>
            </div>

        </section>
        <!-- 底部导航开始 -->
        <footer class="foot-bar">
            <a href="javascript:;" url="${base}/address/getinfo"  class="add_adr">
                新增地址
            </a>
        </footer>
        <!-- 底部导航结束 -->
    </div>
    <!-- 引入jQuery 2.0+ -->
    <script src="/static/scripts/libs/jquery.min.js"></script>
    <!-- 引入YDUI脚本 -->
    <script src="/static/scripts/libs/ydui.js"></script>
    <script src="/static/scripts/libs/dropload.js"></script>
    <script src="/static/scripts/apps/address.js"></script>
</body>
<script>
    $(function () {
        // 页数
        var page = 1;
        // 每页展示10个
        var size = 5

        // dropload
        $('.content').dropload({
            scrollArea: window,
            domUp: {
                domClass: 'dropload-up',
                domRefresh: '<div class="dropload-refresh">↓下拉刷新-自定义内容</div>',
                domUpdate: '<div class="dropload-update">↑释放更新-自定义内容</div>',
                domLoad: '<div class="dropload-load"><span class="loading"></span>加载中-自定义内容...</div>'
            },
            domDown: {
                domClass: 'dropload-down',
                domRefresh: '<div class="dropload-refresh">↑上拉加载更多-自定义内容</div>',
                domLoad: '<div class="dropload-load"><span class="loading"></span>加载中-自定义内容...</div>',
                domNoData: '<div class="dropload-noData">暂无数据-自定义内容</div>'
            },
            //下拉
            loadUpFn: function (me) {
                $.ajax({
                    type: 'GET',
                    url: '${base}/address/getlist?uid=${uid}&page=' + page + '&size=' + size,
                    dataType: 'json',
                    success: function (resp) {
                        var shtml = '';
                       	var data = resp.data;
                       	if(null != data && data.length > 0){
	                        for (var i = 0; i < data.length; i++) {
	                            shtml += '<div class="address-item" addressId="'+data[i].id+'">';
	                            shtml += '    <div class="adr-info">';
	                            shtml += '        <div class="content_lists">';
	                            shtml += '            <span class="name">';
	                            shtml += data[i].name;
	                            shtml += '            </span>';
	                            shtml += '            <span class="phonenum">';
	                            shtml += data[i].mobile               ;
	                            shtml += '            </span>';
	                            shtml += '        </div>';
	                            shtml += '        <div class="adr-detail">';
	                            shtml += data[i].preAddress+data[i].address;
	                            shtml += '        </div>';
	                            shtml += '    </div>';
	                            shtml += '    <div class="adr_action">';
	                            shtml += '        <div class="selected">';
	                            if(data[i].isDefault == 1){
		                            shtml += '              <div id="selDefault" class="selected selected_item on" addressId="'+dat[i].id+'"></div>';
	                            }else{
	                            	shtml += '              <div id="selDefault" class="selected selected_item" addressId="'+dat[i].id+'"></div>';
	                            }
	                            shtml += '              <label>默认地址</label>';
	                            shtml += '        </div>';
	                            shtml += '        <a href="javascript:;" url="${base}/address/getinfo?id='+data[i].id+'" class="edit">';
	                            shtml += '            <i class="icon-compose"></i> 编辑';
	                            shtml += '        </a>';
	                            shtml += '        <div class="del" addressId="'+data[i].id+'">';
	                            shtml += '            <i class="icon-delete"></i>删除';
	                            shtml += '        </div>';
	                            shtml += '    </div>';
	                            shtml += '</div>';
	                            
	                        }
	                        // 为了测试，延迟1秒加载
	                        setTimeout(function () {
	                            $('.lists').html("").prepend(shtml);
	                            // 每次数据加载完，必须重置
	                            me.resetload();
	                            // 重置页数，重新获取loadDownFn的数据
	                            page = 0;
	                            // 解锁loadDownFn里锁定的情况
	                            me.unlock();
	                            me.noData(false);
	                        }, 1000);
                       	}
                    },
                    error: function (xhr, type) {
                        //alert('Ajax error!');
                        // 即使加载出错，也得重置
                        me.resetload();
                    }
                });
            },
            loadDownFn: function (me) {
                page++;
                // 拼接HTML
                var shtml = '';
                $.ajax({
                	type: 'GET',
                    url: '${base}/address/getlist?uid=${uid}&page=' + page + '&size=' + size,
                    dataType: 'json',
                    success: function (resp) {
                        var shtml = '';
                       	var data = resp.data;
                       	if(null != data && data.length > 0){
	                        for (var i = 0; i < data.length; i++) {
	                            shtml += '<div class="address-item" addressId="'+data[i].id+'">';
	                            shtml += '    <div class="adr-info">';
	                            shtml += '        <div class="content_lists">';
	                            shtml += '            <span class="name">';
	                            shtml += data[i].name;
	                            shtml += '            </span>';
	                            shtml += '            <span class="phonenum">';
	                            shtml += data[i].mobile               ;
	                            shtml += '            </span>';
	                            shtml += '        </div>';
	                            shtml += '        <div class="adr-detail">';
	                            shtml += data[i].preAddress+data[i].address;
	                            shtml += '        </div>';
	                            shtml += '    </div>';
	                            shtml += '    <div class="adr_action">';
	                            shtml += '        <div class="selected">';
	                            if(data[i].isDefault == 1){
		                            shtml += '              <div id="selDefault" class="selected selected_item on" addressId="'+data[i].id+'"></div>';
	                            }else{
	                            	shtml += '              <div id="selDefault" class="selected selected_item" addressId="'+data[i].id+'"></div>';
	                            }
	                            shtml += '              <label>默认地址</label>';
	                            shtml += '        </div>';
	                            shtml += '        <a href="javascript:;" url="${base}/address/getinfo?id='+data[i].id+'" class="edit">';
	                            shtml += '            <i class="icon-compose"></i> 编辑';
	                            shtml += '        </a>';
	                            shtml += '        <div class="del" addressId="'+data[i].id+'">';
	                            shtml += '            <i class="icon-delete" ></i>删除';
	                            shtml += '        </div>';
	                            shtml += '    </div>';
	                            shtml += '</div>';
	                            
	                        }
                            // 如果没有数据
                        } else {
                            // 锁定
                            me.lock();
                            // 无数据
                            me.noData();
                        }
                        // 为了测试，延迟1秒加载
                        setTimeout(function () {
                            // 插入数据到页面，放到最后面
                            $('.lists').append(shtml);
                            // 每次数据插入，必须重置
                            me.resetload();
                        }, 1000);
                    },
                    error: function (xhr, type) {
                        //alert('Ajax error!');
                        // 即使加载出错，也得重置
                         me.lock();
                            // 无数据
                            me.noData();
                        me.resetload();
                    }
                });
            },
            threshold: 50
        });
    });
    
    var dialog = window.YDUI.dialog;
    //选择
    $(document).on('click', '.selected_item', function(){
    	var selectFlag = 0;
        if($(this).hasClass('on')){
            $('.selected_item').removeClass('on')
            //选中状态
            $(this).removeClass('on')
            selectFlag = 1;
        }else{
            $('.selected_item').removeClass('on')
            //未选中
            $(this).addClass('on')
        }
        $.ajax({
            type: 'POST',
            url: '${base}/address/setdefault?uid='+uid+'&addressId='+$(this).attr("addressId")+'&selectFlag='+selectFlag,
            dataType: 'json',
            success: function (resp) {
            	dialog.toast('设置成功', 'none', 1000);
            },
            error: function (xhr, type) {
            	dialog.toast('设置失败', 'none', 1000);
            }
        });
    })
    function getUrlParam(name){
	    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
	    var r = window.location.search.substr(1).match(reg);
	    if (r!=null) return unescape(r[2]); return null;
	}
	var backFlag = getUrlParam('backFlag');
	if(null != backFlag && backFlag == 1){
		var goodsId = getUrlParam('goodsId');
		var buyType = getUrlParam('buyType');
		var buyNum = getUrlParam('buyNum');
	    $(document).on('click', '.adr-info', function(){
	    	window.location.href = '${base}/order/preorder?goodsId='+goodsId+'&buyType='+buyType+'&buyNum='+buyNum+'&userId='+uid+'&addressId='+$(this).attr("addressId");
	    })
	    $(document).on('click', '.edit', function(){
	    	window.location.href = $(this).attr("url")+'&goodsId='+goodsId+'&buyType='+buyType+'&buyNum='+buyNum+'&userId='+uid+'&backFlag=1';
	    })
	    $(document).on('click', '.add_adr', function(){
	    	window.location.href = $(this).attr("url")+'?goodsId='+goodsId+'&buyType='+buyType+'&buyNum='+buyNum+'&userId='+uid+'&backFlag=1';
	    })
	}else if(null != backFlag && backFlag == 2){
		var cartIds = getUrlParam('cartIds');
	    $(document).on('click', '.adr-info', function(){
	    	window.location.href = '${base}/order/precartorder?cartIds='+cartIds+'&userId='+uid+'&addressId='+$(this).attr("addressId");
	    })
	    $(document).on('click', '.edit', function(){
	    	window.location.href = $(this).attr("url")+'&cartIds='+cartIds+'&userId='+uid+'&backFlag=2';
	    })
	    $(document).on('click', '.add_adr', function(){
	    	window.location.href = $(this).attr("url")+'?cartIds='+cartIds+'&userId='+uid+'&backFlag=2';
	    })
	}else{
		$(document).on('click', '.edit', function(){
	    	window.location.href = $(this).attr("url");
	    })
	    $(document).on('click', '.add_adr', function(){
	    	window.location.href = $(this).attr("url");
	    })
	}
	
    
    //删除
    $(document).on('click', '.del', function(){
        let that = $(this)
        /* 普通确认框 */
        dialog.confirm('确定删除吗？','', function () {
        	$.ajax({
                type: 'POST',
                url: '${base}/address/delete?uid='+uid+'&addressId='+that.attr("addressId"),
                dataType: 'json',
                success: function (resp) {
                	dialog.toast('已经删除了', 'none', 1000);
                    that.parent().parent().remove()
                },
                error: function (xhr, type) {
                	alert('删除失败');
                }
            });
            //有真是数据以后最好reload ↓
            //location.reload()
        });
        
    })
</script>
</html>
